package com.example.demo.web;

import com.example.demo.Service.UserService;
import com.example.demo.annotation.Encrypt;
import com.example.demo.config.SecretKeyConfig;
import com.example.demo.mongo.Attention;
import com.example.demo.mongo.Follow;
import com.example.demo.mongo.User;
import com.example.demo.mongo.UserDto;
import com.example.demo.mongo.dao.AttentionRepoistory;
import com.example.demo.mongo.dao.FollowRepoistory;
import com.example.demo.mongo.dao.UserRepoistory;
import com.example.demo.resp.RespMsg;
import com.example.demo.util.Base64Util;
import com.example.demo.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * @author wujunyu
 * @date 2020/9/9 16:45
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserRepoistory userRepoistory;

    @Autowired
    private FollowRepoistory followRepoistory;

    @Autowired
    private AttentionRepoistory attentionRepoistory;

    @Autowired
    private SecretKeyConfig secretKeyConfig;

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public Mono<RespMsg> getUser(String userName) {
        return userRepoistory.findByUserName(userName).map(RespMsg::success);
    }


    @GetMapping("/userInfo")
    public Mono<RespMsg> getUserInfo() {
        return Mono.just(userService.findAll()).map(RespMsg::success);
    }


    @PostMapping("/save")
    public Mono<RespMsg> saveUser(@RequestBody User user) {
        return userRepoistory.save(user).map(RespMsg::success);
    }

    @PostMapping("/saveFollow")
    public Flux<RespMsg> saveFollow() {
        Flux<User> userFlux = userRepoistory.findAll();
        return userFlux.flatMap(x -> {
            Follow follow = new Follow();
            follow.setUserId(x.getUserId());
            follow.setFollowId("aaaaaaaaa");
            followRepoistory.save(follow).subscribe();
            Attention attention = new Attention();
            attention.setUserId(x.getUserId());
            attention.setAttentionId("bbbbbbb");
            attentionRepoistory.save(attention).subscribe();
            return Flux.just();
        }).map(RespMsg::success);
    }

    @GetMapping("/get")
    public Mono<RespMsg> findAll() {
        return userService.findAll().map(RespMsg::success);
                
    }

    private RespMsg encry(User content) {
        String publicKey = secretKeyConfig.getPublicKey();
        if (!StringUtils.hasText(publicKey)) {
            throw new NullPointerException("Please configure rsa.encrypt.privatekeyc parameter!");
        }
        byte[] data = content.toString().getBytes();
        String response = null;
        try {
            byte[] encodedData = RSAUtil.encrypt(data, publicKey);
            response = Base64Util.encode(encodedData);
        } catch (Exception e) {
            log.error("加密异常,e:{}", e);
        }
        if (secretKeyConfig.isShowLog()) {
            log.info("Pre-encrypted data：{}，After encryption：{}", content, response);
        }
        log.info("加密后,re:{}", response);
        return RespMsg.success(response);
    }
}
